'use strict';
import {AlertModule} from 'vux'
export let Util = {

    trim: (text) => {
        return text
            .toString()
            .replace(/^\s+|\s+$/gm, '');
    },

    verifyPhone: (text) => {
        if (!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(text))) {
            return false;
        }
        return true;
    },

    // get url's query
    getUrlParam: function (name) {
        let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        let r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return decodeURI(r[2]);
        } else {
            return null;
        }
    },

    // Ajax error warning
    errorWarning(self) {
        self
            .$vux
            .loading
            .hide();
        AlertModule.show(
            { title: '系统提示', content: 'Ajax请求失败，请刷新重试或联系管理员', onShow() { }, onHide() { } }
        );
    },

    setFooterClass(self) {
        // set footer's class
        self.footer.type == 'default'
            ? self.footer.isDefault = true
            : self.footer.isDefault = false;
        self.footer.type == 'detail'
            ? self.footer.isDetail = true
            : self.footer.isDetail = false;
        self.footer.type == 'download'
            ? self.footer.isDownload = true
            : self.footer.isDownload = false;
    },

    setSession(data, info="MeiJingKu_info") {
        return sessionStorage.setItem(info, JSON.stringify(data));
    },

    getSession(info="MeiJingKu_info") {
        return JSON.parse(sessionStorage.getItem(info));
    },

    setHistory(_self) {
        let _session = Util.getSession('MeiJingKu_history'),
            openid = _session.openid,
            title = _session.title,
            message = _session.message,
            picture = _session.picture,
            link = escape(location.href),
            browseRecords = JSON.stringify({
                "openid": openid,
                "title": title,
                "message": message,
                "picture": picture,
                "link": link
            }),
            config = {
                headers: {
                    'Content-Type': 'application/json'
                }
            };
        _self.$http.post('user/UploadBrowseRecords/v1', browseRecords, config).then(response => {
            let result = response.data;
        }, response => {
            Util.errorWarning(_self);
        });
    }

}